package cn.com.ebidding.business.service.impl;

import cn.com.ebidding.business.entity.Group;
import cn.com.ebidding.business.entity.User;
import cn.com.ebidding.business.entity.UserPwd;
import cn.com.ebidding.business.enums.UserStatusEnum;
import cn.com.ebidding.business.service.SysGroupService;
import cn.com.ebidding.business.vo.GroupConditionVO;
import cn.com.ebidding.business.vo.UserConditionVO;
import cn.com.ebidding.framework.exception.ZhydCommentException;
import cn.com.ebidding.framework.exception.ZhydException;
import cn.com.ebidding.framework.holder.RequestHolder;
import cn.com.ebidding.persistence.beans.SysGroup;
import cn.com.ebidding.persistence.beans.SysUser;
import cn.com.ebidding.persistence.mapper.SysGroupMapper;
import cn.com.ebidding.util.IpUtil;
import cn.com.ebidding.util.PasswordUtil;
import cn.com.ebidding.util.SessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户
 *
 * @author 扫地僧 (saods@ebidding.com.cn)
 * @version 1.0
 * @website https://ksp.ebidding.com.cn
 * @date 2018/4/16 16:26
 * @since 1.0
 */
@Service
public class SysGroupServiceImpl implements SysGroupService {

    @Autowired
    private SysGroupMapper sysGroupMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Group insert(Group group) {
        Assert.notNull(group, "Group不可为空！");
        group.setUpdateTime(new Date());
        group.setCreateTime(new Date());
        group.setStatus(UserStatusEnum.NORMAL.getCode());
        group.setUpdateUser(SessionUtil.getUser().getId());
        group.setCreateUser(SessionUtil.getUser().getId());
        sysGroupMapper.insertSelective(group.getSysGroup());
        return group;
    }

    @Override
    public Group getByGroupName(String name) {
        Group group = new Group();
        group.setName(name);
        SysGroup sysGroup = this.sysGroupMapper.selectOne(group.getSysGroup());
        return null == sysGroup ? null : new Group(sysGroup);
    }

    @Override
    public List<Map<String, Object>> queryGroupListWithSelected(Long articleId,Integer authType) {
        List<Map<String, Object>> list = sysGroupMapper.queryGroupListWithSelected(articleId, authType);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        for (Map<String, Object> map : list) {
            map.put("pId", 0);
            if("1".equals(map.get("selected").toString())) {
                map.put("checked",true);
            }else {
                map.put("checked",false);
            }
        }

        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertList(List<Group> users) {
        Assert.notNull(users, "Groups不可为空！");
        List<SysGroup> sysGroups = new ArrayList<>();
        for (Group group : users) {
            group.setUpdateTime(new Date());
            group.setCreateTime(new Date());
            group.setStatus(UserStatusEnum.NORMAL.getCode());
            group.setUpdateUser(SessionUtil.getUser().getId());
            group.setCreateUser(SessionUtil.getUser().getId());
            sysGroups.add(group.getSysGroup());
        }
        sysGroupMapper.insertList(sysGroups);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return sysGroupMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSelective(Group group) {
        Assert.notNull(group, "Group不可为空！");
        group.setUpdateTime(new Date());
        group.setUpdateUser(SessionUtil.getUser().getId());
        return sysGroupMapper.updateByPrimaryKeySelective(group.getSysGroup()) > 0;
    }

    @Override
    public Group getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空！");
        SysGroup sysUser = sysGroupMapper.selectByPrimaryKey(primaryKey);
        return null == sysUser ? null : new Group(sysUser);
    }

    @Override
    public List<Group> listAll() {
        List<SysGroup> sysGroups = sysGroupMapper.selectAll();

        if (CollectionUtils.isEmpty(sysGroups)) {
            return null;
        }
        List<Group> groups = new ArrayList<>();
        for (SysGroup sysGroup : sysGroups) {
            groups.add(new Group(sysGroup));
        }
        return groups;
    }

    @Override
    public PageInfo<Group> findPageBreakByCondition(GroupConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<SysGroup> sysGroups = sysGroupMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(sysGroups)) {
            return null;
        }
        List<Group> groups = new ArrayList<>();
        for (SysGroup su : sysGroups) {
            groups.add(new Group(su));
        }
        PageInfo bean = new PageInfo<SysGroup>(sysGroups);
        bean.setList(groups);
        return bean;
    }


}
